﻿/// <reference path="D:\PROJECTS\eking\Modules\Orchard.jQuery\Scripts\jquery-1.6.4.min.js" />

$.fn.jXamlGrid = function (options) {
    var opts = $.extend({}, $.fn.jXamlGrid.defaults, options);
    return this.each(function () {
        handleGrid(this);
    });

    function handleGrid(input) {
        var grid = $(input);
        grid.css("position", "relative");
        grid.addClass('jXamlGrid');
        positionChildren(grid);
        grid.resize(function () {
            positionChildren(grid);
        });

        var currentSplittingSplitter = undefined;
        //var initMousePosition = undefined;
        grid.children(".jXamlGridSplitter").mousedown(function (evt) {
            currentSplittingSplitter = this;
            if (currentSplittingSplitter.initMousePosition == undefined)
                currentSplittingSplitter.initMousePosition = { "x": evt.screenX, "y": evt.screenY };
        });

        $(document).mouseup(function (evt) {
            if (currentSplittingSplitter == undefined)
                return;
            currentSplittingSplitter = undefined;
        });

        $(document).mousemove(function (evt) {
            if (currentSplittingSplitter == undefined)
                return;

            var currentMousePosition = { "x": evt.screenX, "y": evt.screenY };
            doSplit(grid, currentSplittingSplitter, currentSplittingSplitter.initMousePosition, currentMousePosition);
            currentSplittingSplitter.initMousePosition = currentMousePosition;
        });

    }

    function doSplit(grid, splitter, initMousePosition, currentMousePosition) {
        if (grid.params == undefined)
            grid.params = calGridRowCol(grid);
        var params = grid.params;
        splitter = $(splitter);
        var row = parseInt(splitter.attr("data-gridRow"));
        var col = parseInt(splitter.attr("data-gridColumn"));


        var isV = splitter.hasClass('V');
        var isH = splitter.hasClass('H');
        if (isH) {
            var delta = currentMousePosition.x - initMousePosition.x;
            params.cols[col - 1].Width = params.cols[col - 1].Width + delta;
            params.cols[col + 1].Width = params.cols[col + 1].Width - delta;
        }
        if (isV) {
            var delta = currentMousePosition.y - initMousePosition.y;
            params.rows[row - 1].Height = params.rows[row - 1].Height + delta;
            params.rows[row + 1].Height = params.rows[row + 1].Height - delta;
        }

        positionChildren(grid, params);
    }

    function positionChildren(grid, params) {
        if (params == undefined)
            params = calGridRowCol(grid);

        grid.children().each(function () {
            positionChild(grid, $(this), params);
        });
    }

    function calGridRowCol(grid) {
        var gWidth = grid.width();
        var gHeight = grid.height();
        var props = opts.gridDef;


        var cols = [];
        var items = [];
        for (var i = 0; i < props.ColumnDefinitions.length; i++) {
            items.push(props.ColumnDefinitions[i].Width);
        }
        var sizes = devide(gWidth, items);
        for (var i = 0; i < sizes.length; i++) {
            cols.push({ Width: sizes[i] });
        }

        items = [];
        for (var i = 0; i < props.RowDefinitions.length; i++) {
            items.push(props.RowDefinitions[i].Height);
        }

        sizes = devide(gHeight, items);
        var rows = [];
        for (var i = 0; i < sizes.length; i++) {
            rows.push({ Height: sizes[i] });
        }

        return { "cols": cols, "rows": rows };
    }

    function devide(size, itemSize) {
        var output = [itemSize.length];
        var remainSize = size;
        for (var i = 0; i < itemSize.length; i++) {
            if (itemSize[i].contains("px")) {
                s = parseInt(itemSize[i].replace("px", ""));
                output[i] = s;
                remainSize -= s;
            }
        }

        for (var i = 0; i < itemSize.length; i++) {
            if (itemSize[i].contains("%")) {
                s = parseInt(itemSize[i].replace("%", "")) / 100 * remainSize;
                output[i] = s;
                //remainSize -= s;
            }
        }

        return output;
    }

    function parseMargin(input) {
        if (input == undefined)
            input = '0px';

        if (input.contains(',')) {
            var mg = input.split(',');
            return {
                Left: parseInt(jQuery.trim(mg[3].replace("px", ""))),
                Top: parseInt(jQuery.trim(mg[0].replace("px", ""))),
                Right: parseInt(jQuery.trim(mg[1].replace("px", ""))),
                Bottom: parseInt(jQuery.trim(mg[2].replace("px", "")))
            };
        }

        return { Left: parseInt(input), Top: parseInt(input), Right: parseInt(input), Bottom: parseInt(input) };
    }

    function positionChild(grid, child, params) {
        //var gridProps = child.attr("data-Grid");
        var row = parseInt(child.attr("data-gridRow"));
        var col = parseInt(child.attr("data-gridColumn"));
        var rowSpan = child.attr("data-gridRowSpan");
        var colSpan = child.attr("data-gridColumnSpan");

        var margin = parseMargin(child.attr("data-gridMargin"));

        if (row == undefined || col == undefined)
            return;
        rowSpan = rowSpan == undefined ? 1 : parseInt(rowSpan);
        colSpan = colSpan == undefined ? 1 : parseInt(colSpan);

        var top = 0;
        for (var i = 0; i < row; i++) {
            top += params.rows[i].Height;
        }

        var left = 0;
        for (var i = 0; i < col; i++) {
            left += params.cols[i].Width;
        }

        var width = 0;
        for (var i = col; i < col + colSpan; i++) {
            width += params.cols[i].Width;
        }

        var height = 0;
        for (var i = row; i < row + rowSpan; i++) {
            height += params.rows[i].Height;
        }
        left += margin.Left;
        top += margin.Top;
        width -= margin.Left + margin.Right;
        height -= margin.Top + margin.Bottom;

        child.css("position", "absolute");
        child.css("left", left + "px");
        child.css("top", top + "px");
        child.css("width", width + "px");
        child.css("height", height + "px");
    }
}

$.fn.jXamlGrid.defaults = {
    gridDef: undefined,
    childrenDef: undefined

}

Grid: { Row: 1; Column: 1; RowSpan: 2; ColumnSpan: 2; Margin: '42,81,0,0' }

String.prototype.contains = function (it) { return this.indexOf(it) != -1; };
